package com.woniu.controller;

import com.woniu.common.CommonResult;
import com.woniu.pojo.*;
import com.woniu.service.ServiceStateService;
import com.woniu.service.ServiceStopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.woniu.service.ServiceOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.sound.midi.Soundbank;


@Controller
@RequestMapping("/payorder")
public class PayOrderController {


    @Autowired
    private ServiceOrderService serviceOrderService;

    @Autowired
    private ServiceStopService serviceStopService;

    @Autowired
    private ServiceStateService serviceStateService;

    @PostMapping("/createorder")
    public @ResponseBody
    CommonResult createorder(String starttime, String endtime, Integer serviceid,String payname) {
        //获得SimpleDateFormat类，我们转换为yyyy-MM-dd的时间格式
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String ordernumber="";
        Random random=new Random();
        for(int i=0;i<3;i++){
           ordernumber+=random.nextInt(10);
        }
        ordernumber+=newDate;
        System.out.println(ordernumber);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        //使用SimpleDateFormat的parse()方法生成Date
        Date start = null;
        try {
            start = sf.parse(starttime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date end = null;
        try {
            end = sf.parse(endtime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(start);
        BigInteger order = null;
        Integer userid = 1;
        ServiceOrder serviceOrder = new ServiceOrder();
        serviceOrder.setServiceid(serviceid);
        serviceOrder.setUserid(userid);
        serviceOrder.setStarttime(start);
        serviceOrder.setEndtime(end);
        serviceOrder.setOrdernumber(ordernumber);
        serviceOrder.setStatus(6);
        serviceOrder.setOrdername(payname);
        serviceOrder.setOrdertype(0);
        try {
            Integer Serviceorderid=serviceOrderService.addServiceOrder(serviceOrder);
            return CommonResult.success("生成订单成功",Serviceorderid);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("生成订单失败");
        }
    }

    @PostMapping("/addppay")
    public @ResponseBody CommonResult addppayorder(Ppay ppay)
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String ordernumber="";
        Random random=new Random();
        for(int i=0;i<3;i++){
            ordernumber+=random.nextInt(10);
        }
        ordernumber+=newDate;
        ppay.setOrdernumber(ordernumber);
        ppay.setCreatetime(new Date());
        ppay.setOrdername(ppay.getPayname());
        Integer userid=1;
        ppay.setUserid(userid);
        ppay.setOrdertype(0);
        ppay.setStatus(0);
        try {
            Integer ppayid=serviceOrderService.addPpay(ppay);
            return CommonResult.success("生成订单成功",ppayid);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("添加订单失败");
        }
    }


    @GetMapping("/selectspoke")
    public @ResponseBody
    CommonResult selectspoke() {
        Integer userid = 1;
        try {
            List<ServiceOrderdto> serviceOrderdtos = serviceOrderService.selectspoke(userid);
            return CommonResult.success(serviceOrderdtos);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("获取订单失败");
        }
    }

    @GetMapping("/selectattend")
    public @ResponseBody
    CommonResult selectattend() {
        Integer userid = 1;
        try {
            List<ServiceOrderdto> serviceOrderdtos = serviceOrderService.selectattend(userid);
            return CommonResult.success(serviceOrderdtos);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("获取订单失败");
        }
    }

    @GetMapping("/selectreturn")
    public @ResponseBody
    CommonResult selectreturn() {
        Integer userid = 1;
        try {
            List<ServiceOrderdto> serviceOrderdtos = serviceOrderService.selectreturn(userid);
            return CommonResult.success(serviceOrderdtos);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("获取订单失败");
        }
    }

    @GetMapping("/selectwait")
    public @ResponseBody
    CommonResult selectwait() {
        Integer userid = 1;
        try {
            List<ServiceOrderdto> serviceOrderdtos = serviceOrderService.selectwait(userid);
            return CommonResult.success(serviceOrderdtos);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("获取订单失败");
        }
    }

    @GetMapping("/selectwaitpay")
    public @ResponseBody
    CommonResult selectwaitpay() {
        Integer userid = 1;
        try {
            List<ServiceOrderdto> serviceOrderdtos = serviceOrderService.selectwaitpay(userid);
            return CommonResult.success(serviceOrderdtos);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("获取订单失败");
        }
    }

    @GetMapping("/selectstate")
    public @ResponseBody
    CommonResult selectstate() {
        Integer userid = 1;
        try {
            List<ServiceOrderdto> serviceOrderdtos = serviceOrderService.selectstate(userid);
            return CommonResult.success(serviceOrderdtos);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("获取订单失败");
        }
    }

    @GetMapping("/selectrefuse")
    public @ResponseBody
    CommonResult selectrefuse() {
        Integer userid = 1;
        try {
            List<ServiceOrderdto> serviceOrderdtos = serviceOrderService.selectrefuse(userid);
            return CommonResult.success(serviceOrderdtos);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("获取订单失败");
        }
    }

    @GetMapping("/selectcomplete")
    public @ResponseBody
    CommonResult selectcomplete() {
        Integer userid = 1;
        try {
            List<ServiceOrderdto> serviceOrderdtos = serviceOrderService.selectcomplete(userid);
            return CommonResult.success(serviceOrderdtos);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("获取订单失败");
        }
    }

    @PostMapping("/addstop")
    public @ResponseBody
    CommonResult stoporder(StopService stopService) {
        stopService.setStatus(0);
        ServiceOrder serviceOrder = serviceOrderService.selectServiceorderByid(stopService.getServiceid());
        try {
            serviceStopService.addStopService(stopService);
            serviceOrder.setStatus(3);
            return CommonResult.success("取消订单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("取消订单失败");
        }
    }

    @PostMapping("/addstate")
    public @ResponseBody
    CommonResult orderstate(StateService stateService) {
        stateService.setStatus(0);
        ServiceOrder serviceOrder = serviceOrderService.selectServiceorderByid(stateService.getServiceid());
        try {
            serviceStateService.addServiceState(stateService);
            serviceOrder.setStatus(2);
            return CommonResult.success("订单投诉成功");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("订购投诉失败");
        }
    }


    @GetMapping("getppaydetail/{ppayid}")
    public @ResponseBody CommonResult getppay(@PathVariable("ppayid")Integer ppayid)
    {
        try {
            Ppay ppay=serviceOrderService.selectbyid(ppayid);
            return  CommonResult.success(ppay);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("获取数据失败");
        }
    }
}







